package com.andersen.database.datatable;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Corvin on 27.06.14.
 */
@Component
public class TableParamParser {

    private static final Logger LOG = Logger.getLogger(TableParamParser.class);

    private static final String PARAM_NAME_OFFSET = "iDisplayStart";
    private static final String PARAM_NAME_COUNT = "iDisplayLength";
    private static final String PARAM_NAME_SORT_COLUMN = "iSortCol_0";
    private static final String PARAM_NAME_SORT_ORDER = "sSortDir_0";
    private static final String SORT_ORDER_ASC = "asc";

    public TableParam parseRequest(HttpServletRequest request) {
        TableParam params = new TableParam();

        try {
            params.setCount(Integer.parseInt(request.getParameter(PARAM_NAME_COUNT)));
            params.setOffset(Integer.parseInt(request.getParameter(PARAM_NAME_OFFSET)));
            params.setSortColumnNumber(Integer.parseInt(request.getParameter(PARAM_NAME_SORT_COLUMN)));

            params.setSortAsc(SORT_ORDER_ASC.equals(request.getParameter(PARAM_NAME_SORT_ORDER)));
        } catch (NumberFormatException formatExc) {
            params = null;
            LOG.error("TableParamParser.parseRequest() error:",formatExc);
        }
        return params;
    }


}
